<?php

namespace App\Providers;

use Illuminate\Database\Query\Builder;
use Illuminate\Support\ServiceProvider;

class ModelServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        Builder::macro('searchWhere', function ($cron) {
//            print_r($cron['where']);
            foreach ($cron as $key => $value) {
                $key = strtoupper($key);
                switch ($key) {
                    case 'WHERE':
                    case 'EQ':
//                        print_r($value);
                        foreach ($value as $k => $v) {
                            if (is_array($v)) {
                                $nums = count($v);
                                if ($nums >= 3) {
                                    $this->where($v[0], $v[1], $v[2]);
                                } else {
                                    $this->where($k, $v[0], $v[1]);
                                }
                            } else {
                                $this->where($k, '=', $v);
                            }
                        }
                        break;
                    case 'REGEXP':
                        foreach ($value as $k => $v) {
                            $this->where($k, 'REGEXP', $v);
                        }
                        break;
                    case 'EGT':
                        foreach ($value as $k => $v) {
                            $this->where($k, '>=', $v);
                        }
                        break;
                    case 'GT':
                        foreach ($value as $k => $v) {
                            $this->where($k, '>', $v);
                        }
                        break;
                    case 'LT':
                        foreach ($value as $k => $v) {
                            $this->where($k, '<', $v);
                        }
                        break;
                    case 'ELT':
                        foreach ($value as $k => $v) {
                            $this->where($k, '<=', $v);
                        }
                        break;
                    case 'RAW':
                        if (is_string($value)) {
                            $this->whereRaw($value);
                        } else if (is_array($value)) {
                            foreach ($value as $k => $v) {
                                $this->whereRaw($v);
                            }
                        }
                        break;
                    case 'ORRAW':
                        if (is_string($value)) {
                            $this->orWhereRaw($value);
                        } else if (is_array($value)) {
                            foreach ($value as $k => $v) {
                                $this->orWhereRaw($v);
                            }
                        }
                        break;
                    case 'EMPTY':
                        if (is_string($value)) {
                            $this->where($value, '=', "");
                        } else if (is_array($value)) {
                            foreach ($value as $k => $v) {
                                $this->where($v, '=', "");
                            }
                        }
                        break;
                    case 'NOTEMPTY':
                        if (is_string($value)) {
                            $this->where($value, '!=', "");
                        } else if (is_array($value)) {
                            foreach ($value as $k => $v) {
                                $this->where($v, '!=', "");
                            }
                        }
                        break;
                    case 'ORNULL':
                    case 'ORWHERENULL':
                        if (is_string($value)) {
                            $this->orWhereNull($value);
                        } else if (is_array($value)) {
                            foreach ($value as $k => $v) {
                                $this->orWhereNull($v);
                            }
                        }
                        break;
                    case 'ORNOTNULL':
                    case 'ORWHERENOTNULL':
                        if (is_string($value)) {
                            $this->orWhereNotNull($value);
                        } else if (is_array($value)) {
                            foreach ($value as $k => $v) {
                                $this->orWhereNotNull($v);
                            }
                        }
                        break;
                    case 'WHERENULL':
                    case 'ISNULL':
                        if (is_string($value)) {
                            $this->whereNull($value);
                        } else if (is_array($value)) {
                            foreach ($value as $k => $v) {
                                $this->whereNull($v);
                            }
                        }
                        break;
                    case 'WHERENOTNULL':
                    case 'ISNOTNULL':
                        if (is_string($value)) {
                            $this->whereNotNull($value);
                        } else if (is_array($value)) {
                            foreach ($value as $k => $v) {
                                $this->whereNotNull($v);
                            }
                        }
                        break;
                    case 'WHEREOR':
                    case 'ORWHERE':
                    case 'OR':
                        foreach ($value as $k => $v) {
                            if (is_array($v)) {
                                $nums = count($v);
                                if ($nums >= 3) {
                                    $this->orWhere($v[0], $v[1], $v[2]);
                                } else {
                                    $this->orWhere($k, $v[0], $v[1]);
                                }
                            } else {
                                $this->orWhere($k, '=', $v);
                            }
                        }
                        break;
                    case 'IN':
                        foreach ($value as $k => $v) {
                            $this->whereIn($k, $v);
                        }
                        break;
                    case 'NOTIN':
                        foreach ($value as $k => $v) {
                            $this->whereNotIn($k, $v);
                        }
                        break;
                    case 'NEQ':
                    case 'NOTEQ':
                        foreach ($value as $k => $v) {
                            $this->where($k, '!=', $v);
                        }
                        break;
                    case 'BETWEEN':
                        foreach ($value as $k => $v) {
                            $this->whereBetween($k, $v);
                        }
                        break;
                    case 'LIKE':
//                        这个是原来的写法，留下来吧
                    case 'LIKER':
                        $this->where(function ($query) use ($value) {
                            foreach ($value as $lk => $lv)
                                $query->where($lk, 'like', '%' . $lv . '%');
                        });
                        break;
                    case 'LLIKE':
                    case 'LEFTLIKE':
                    case 'LIKELEFT':
                    case 'STARTLIKE':
                    case 'LIKESTART':
                    case 'LEFT_LIKE':
                    case 'LIKE_LEFT':
                    case 'LIKE_START':
                    case 'START_LIKE':
                        $this->where(function ($query) use ($value) {
                            foreach ($value as $lk => $lv)
                                $query->where($lk, 'like', $lv . '%');
                        });
                        break;
                    case 'RLIKE':
                    case 'RIGHTLIKE':
                    case 'LIKERIGHT':
                    case 'LIKEEND':
                    case 'ENDLIKE':
                    case 'RIGHT_LIKE':
                    case 'LIKE_RIGHT':
                    case 'LIKE_END':
                    case 'END_LIKE':
                        $this->where(function ($query) use ($value) {
                            foreach ($value as $lk => $lv)
                                $query->where($lk, 'like', '%' . $lv);
                        });
                        break;
                    case 'LIKEOR':
                    case 'ORLIKE':
                        $this->where(function ($query) use ($value) {
                            foreach ($value as $lk => $lv)
                                $query->orWhere($lk, 'like', $lv);
                        });
                        break;
//                        旧程序不知道什么意思，抄下来吧
                    case 'LIKEORALL':
                        foreach ($value as $lk => $lv) {
                            $this->where(function ($query) use ($lk, $lv) {
                                if (is_array($lv)) {
                                    foreach ($lv as $kl1 => $vl1) {
                                        $query->orWhere($lk, 'like', '%' . $vl1 . '%');
                                    }
                                }
                                if (is_string($lv)) {
                                    $query->orWhere($lk, 'like', $lv);
                                }
                            });
                        }
                        break;
                    default:
                        if (is_numeric($key) && is_array($value) && count($value) == count($value, 1)) {
//    [['status', '=', 0], ['parent_id', '>', 0]]这种格式条件
                            $this->where([$value]);

                        } else {
//                            ['status'=>0,'id'=>'abc']这种格式条件
                            $this->where([$key => $value]);
                        }

                        break;
                }
            };
            return $this;
        });
        Builder::macro('page', function ($page, $limit) {
            $start = 0;
            if ($page >= 1) {
                $start = ((int)$page - 1) * (int)$limit;
            }
            return $this->skip($start)->take($limit);
        });
    }
}
